﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;

namespace SysObj
{
    public class Kho_KiemKe : Lib.Bases.BaseClass
    {
        Kho_KiemKe_HangHoa _ctiet = new Kho_KiemKe_HangHoa();

        public System.Int32 IdKiemKe
        {
            get;
            set;
        }

        public System.DateTime Ngay
        {
            get;
            set;
        }

        public System.String DienGiai
        {
            get;
            set;
        }

        public DataTable Detail { get; set; }

        public Kho_KiemKe()
        {
            m_DataLayer = new Lib.Data.DataLayer();
        }

        #region Methods
        public override void ResetData()
        {
            IdKiemKe = 0;
            Ngay = DateTime.Now;
            DienGiai = string.Empty;
        }

        public void ReadData()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = m_DataLayer.ExecDataTable("SP_Kho_KiemKe", "Command,IdKiemKe", "ReadData", IdKiemKe);
                this.ResetData();
                if (dt == null || dt.Rows.Count <= 0)
                    return;
                this.FillData(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override DataTable LoadData()
        {
            return m_DataLayer.ExecDataTable("SP_Kho_KiemKe", "Command,IdKiemKe", "LoadData", IdKiemKe);
        }

        public DataTable GetHistory()
        {
            return m_DataLayer.ExecDataTable("SP_Kho_KiemKe", "Command", "GetHistory");
        }

        public DateTime SmallThanNearest(DateTime date)
        {
            return Convert.ToDateTime( m_DataLayer.ExecScalar("SP_Kho_KiemKe", "Command,Ngay", "SmallThanNearest", Cvert.ToShortDate(date)));
        }

        public override void FillData(DataRow row)
        {
            ResetData();
            if (row.Table.Columns.Contains("IdKiemKe"))
                IdKiemKe = Lib.Data.ValueType.GetValue(row["IdKiemKe"], IdKiemKe);
            if (row.Table.Columns.Contains("Ngay"))
                Ngay = Lib.Data.ValueType.GetValue(row["Ngay"], Ngay);
            if (row.Table.Columns.Contains("DienGiai"))
                DienGiai = Lib.Data.ValueType.GetValue(row["DienGiai"], DienGiai);
        }

        public DataTable Get_ChiTiet()
        {
            return _ctiet.GetDetail(m_DataLayer, IdKiemKe);
        }

        public override void OnUpdate()
        {
            Hashtable output = null;
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Update");
                Lib.Data.ParamHolder.AddParam("IdKiemKe", IdKiemKe);
                Lib.Data.ParamHolder.AddParam("Ngay", Cvert.ToShortDate(Ngay));
                Lib.Data.ParamHolder.AddParam("DienGiai", DienGiai);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_Kho_KiemKe", s_ParamName, ref output, o_ParamValue);

                if (!object.Equals(null, output))
                    IdKiemKe = Cvert.ToInt32(output["IdKiemKe"], 0);

                _ctiet.OnDelete(m_DataLayer, IdKiemKe);
                _ctiet.Insert(m_DataLayer, Detail, IdKiemKe);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        public override void OnDelete()
        {
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                _ctiet.OnDelete(m_DataLayer, IdKiemKe);

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Delete");
                Lib.Data.ParamHolder.AddParam("IdKiemKe", IdKiemKe);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_Kho_KiemKe", s_ParamName, o_ParamValue);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        #endregion
    }
}
